package plugins

import (
	"argus_go_server/conf"
	"context"
	"fmt"

	"github.com/go-ini/ini"
	"github.com/redis/go-redis/v9"
)

type RedisServer struct{}

var redisClient *redis.Client // 定义redis客户端
var cfg *ini.File

func init() {
	cfg = conf.GetConf()
	redisClient = redis.NewClient(&redis.Options{
		Addr:     cfg.Section("redis").Key("HOST").String() + ":" + cfg.Section("redis").Key("PORT").String(),
		DB:       cfg.Section("redis").Key("db").MustInt(0),
		Password: cfg.Section("redis").Key("password").String(),
		PoolSize: cfg.Section("redis").Key("pool_size").MustInt(100),
	})
}

// 初始化redis客户端
func (RedisServer) InitRedis() {
	redisClient = redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		DB:       0,
		PoolSize: 100, // 连接池大小
	})

	// 测试连接
	ctx := context.Background()
	pong, err := redisClient.Ping(ctx).Result()
	if err != nil {
		panic(fmt.Sprintf("连接 Redis 失败: %v", err))
	}
	fmt.Println("Redis 连接成功:", pong)
}

func (RedisServer) GetRedisClient() *redis.Client {
	return redisClient
}
